﻿package classes.audio
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite; 
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.net.URLRequest;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	
	import classes.GameSetting;
	
	/**
	 * 音效类
	 * @author rock 
	 * 
	 * 说明：
	 * 播放声音：用play(soudnUrl:String)方法  参数:soudnUrl为要播放的声音的完整路径
	 * 停止声音：用stop(soundUrl:String)方法  参数:soudnUrl为要停止的声音的完整路径
	 * 全部声音停止：用stopAll()方法;
	 */
	public class AudioRockman extends Sprite
	{     
		public static var soundArr:Array = [];
		public static var soundNameArr:Array = []; 
		public static var soundChannelArr:Array = [];
		private static var _bFlg:Boolean = true;
		
		public function AudioRockman()
		{}  
		 
		//播放指定音效
		public static function play(soudnName:String,isBackMusic:Boolean=false):void
		{
			if (_bFlg)
			{
			    try
			    {
			    	var soundClass:Class=GameSetting.audioRsc.contentLoaderInfo.applicationDomain.getDefinition(soudnName) as Class;
			    	var mySound:Sound=new soundClass();    
			       //播放当前成功加载的声音
			       var _sSoundChannel:SoundChannel;
			       if (isBackMusic) 
			       { 
			        	trace("is back music")
				        _sSoundChannel = mySound.play(0,int.MAX_VALUE); 
			       }else {
				        _sSoundChannel = mySound.play(); 
			       }
			
			       soundNameArr.push(soudnName);//保存声音对象
			       soundArr.push(mySound);//保存当前声音对象的名字
			       soundChannelArr.push(_sSoundChannel);//保存当前播放的声音声道对象  
			    }catch (e:*) {
			    	trace(e.type);
			    }
			}
		}
		
		//停止指定的音效 
		public static function stop(soudnName:String):void
		{  
			if (_bFlg)
			{
			    //停止对应的声音文件 
			    for (var i:int = 0; i < soundNameArr.length; i++ )
			    { 
			      	if (soundNameArr[i] == soudnName)
			    	{
				    	soundChannelArr[i].stop();
				    }
			    } 
			}
		} 
		
		//停止所有的音效
		public static function stopAll():void
		{
			_bFlg = false;
			SoundMixer.stopAll();
		}   
		
		//清理数据 
		public function clearData():void
		{
			soundArr = [];
		    soundNameArr = []; 
		    soundChannelArr = [];
		}
	}
}